package com.nervenets.shopping.service.impl;

import com.nervenets.general.enumeration.Action;
import com.nervenets.general.exception.LogicException;
import com.nervenets.general.service.impl.BaseServiceImpl;
import com.nervenets.general.web.params.Params;
import com.nervenets.shopping.hibernate.dao.ManagerDao;
import com.nervenets.shopping.hibernate.domain.user.Manager;
import com.nervenets.shopping.hibernate.domain.user.RoleGroup;
import com.nervenets.shopping.service.ManagerService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManagerServiceImpl extends BaseServiceImpl<Manager, ManagerDao> implements ManagerService {
    @Override
    protected void before(Action action, Manager manager, Params... params) {
        switch (action) {
            case add:
                break;
            case update:
            case delete:
                checkInit(manager);
                break;
        }
    }

    private void checkInit(Manager manager) {
        Manager top1 = dao.findTop1ByOrderById();
        if (null != top1 && top1.getId().equals(manager.getId())) {
            throw new LogicException("为保障安全，初始化账号不允许修改，请重新创建账号！");
        }
    }

    @Override
    public Manager findByMobile(String mobile) {
        return dao.findByMobile(mobile);
    }

    @Override
    public long countByRoleGroupsId(RoleGroup roleGroup) {
        return dao.countByRoleGroupsId(roleGroup.getId());
    }

    @Override
    public List<Manager> findAllByRoleGroup(RoleGroup roleGroup) {
        return dao.findAllByRoleGroupsId(roleGroup.getId());
    }
}
